# 导入了day2_deepseek不需要以下导入
# # -- coding: utf-8 --
import sys

# import io

# # 设置标准输出的编码为UTF-8
# if sys.stdout.encoding != "UTF-8":
#     sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")


# 导入我们修复好的DeepSeekClient
try:
    from day2_deepseek import DeepSeekClient, load_environment
except ImportError:
    print("❌ 无法导入 day2_deepseek 模块")
    print("请确保 day2_deepseek.py 文件在同一目录下")
    sys.exit(1)


class DeepSeekChatbot:
    def __init__(self, api_key):
        self.client = DeepSeekClient(api_key)
        self.conversation_history = []

    def chat(self):
        print("🤖 DeepSeek 聊天机器人已启动！")
        print("输入 'quit' 退出程序")
        print("-" * 50)

        while True:
            user_input = input("\n你：").strip()

            if user_input.lower() in ["quit", "exit", "退出"]:
                print("再见！👋")
                break

            if not user_input:
                continue

            print("AI:", end="")
            response = self.client.call_api(user_input)
            print(response)

            # 保存对话历史
            self.conversation_history.append(
                {"user": user_input, "assistant": response}
            )


# 使用示例
if __name__ == "__main__":
    api_key = load_environment()
    if not api_key:
        sys.exit(1)

    try:
        bot = DeepSeekChatbot(api_key)
        bot.chat()

    except KeyboardInterrupt:
        print("\n\n程序被用户中断")

    except Exception as e:
        print(f"程序出错: {e}")
